/*
	This file is part of PropExtractor.

	PropExtractor is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	PropExtractor is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with PropExtractor. If not, see <http://www.gnu.org/licenses/>.
*/

#include "PEIncludes.h"

#include "PEImage.h"

#include "PETools.h"

namespace pe
{

	int PEImageRawCreate(PEImageRaw* a_Target, unsigned int a_Width, unsigned int a_Height)
	{
		a_Target->width = a_Width;
		a_Target->height = a_Height;
		a_Target->bpp = 4;
		a_Target->data = new byte[a_Target->width * a_Target->height * a_Target->bpp];

		return PE_OK;
	}

	int PEImageRawSetPixelAtIndex(PEImageRaw* a_Target, unsigned int a_Index, byte a_Red, byte a_Green, byte a_Blue, byte a_Alpha /*= 255*/)
	{
		a_Target->data[a_Index * 4 + 0] = a_Red;
		a_Target->data[a_Index * 4 + 1] = a_Green;
		a_Target->data[a_Index * 4 + 2] = a_Blue;
		a_Target->data[a_Index * 4 + 3] = a_Alpha;

		return PE_OK;
	}

	int PEImageRawSaveToFile(PEImageRaw* a_Data, const wchar_t* a_FilePath, const char* a_Pattern)
	{
		wchar_t filename[_MAX_PATH];
		size_t filename_length;

		GetPatternedPath(filename, &filename_length, a_Pattern, a_FilePath);
		CheckDirectory(filename);

		//PE_DEBUG("Path: %s", filename);

		FIBITMAP* dib = FreeImage_Allocate(a_Data->width, a_Data->height, 32);
		RGBQUAD color;
		byte* read = a_Data->data;
		for (unsigned int y = a_Data->height - 1; y > 0; y--)
		{
			byte* dst = (byte*)FreeImage_GetScanLine(dib, y);

			byte* src = read;
			for (unsigned int x = 0; x < a_Data->width; x++)
			{
				/*color.rgbRed      = src[2];
				color.rgbGreen    = src[1];
				color.rgbBlue     = src[0];
				color.rgbReserved = src[3];

				FreeImage_SetPixelColor(dib, x, y, &color);*/

				dst[0] = src[0];
				dst[1] = src[1];
				dst[2] = src[2];
				dst[3] = src[3];

				dst += 4;
				src += 4;
			}

			read += (a_Data->width * 4);
		}

		char* save_filename;
		PE_WIDETOSTR(save_filename, filename);
		FreeImage_Save(FIF_PNG, dib, save_filename);

		delete save_filename;

		FreeImage_Unload(dib);

		return 0;
	}

	int PEImageRawDelete(PEImageRaw* a_Data)
	{
		if (!a_Data) { return -1; }

		delete a_Data->data;
		a_Data->bpp = 0;
		a_Data->width = a_Data->height = 0;

		return PE_OK;
	}

	// ================================
	// Image class
	// ================================

	PEImage::PEImage()
	{
		m_Data = NULL;
	}

	PEImage::~PEImage()
	{
		if (m_Data) { delete m_Data; }
	}

	int PEImage::LoadHeader(PEStream* a_Stream)
	{
		return -1;
	}

	int PEImage::LoadBody()
	{
		return -1;
	}

}; // namespace pe